package com.bankcomm.ui;

import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import com.bankcomm.R;
import com.bankcomm.common.ConfigManager;
import com.bankcomm.common.LPMid;
import com.bankcomm.ui.adapter.ArrayWheelAdapter;
import com.bankcomm.ui.specialwidget.OnWheelScrollListener;
import com.bankcomm.ui.specialwidget.WheelView;
import com.bankcomm.ui.views.BCMHomeView;
import com.bankcomm.ui.views.DataStorage;
import com.bankcomm.ui.views.DropGridAdapter;
import com.bankcomm.ui.views.LPCitySelectView;
import com.bankcomm.ui.views.SlidingMenu;
import com.bankcomm.ui.views.SlidingMenu.OnMenuListener;
import com.bankcomm.util.LPUtils;
import com.bankcomm.util.SecretUtil;
import com.rytong.tools.crypto.AESCipher;
import com.rytong.tools.datastorage.PackageManagerAndroid;
import com.rytong.tools.httpconnect.HttpManager;
import com.rytong.tools.httpconnect.WaitDialog;
import com.rytong.tools.httpconnect.WaitDialog.Task;
import com.rytong.tools.ui.BaseView;
import com.rytong.tools.utils.InputMethodUtil.onCloseImmListener;
import com.rytong.tools.utils.EventHandler;
import com.rytong.tools.utils.LPToastUtil;
import com.rytong.tools.utils.Utils;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * 交行2013版客户端首页
 * 
 * @author chengli  @date 2013.5.14
 */
public class BOCMainHome extends BaseViewCopy implements OnClickListener {
	/**
	 * 发送请求类型
	 */
	private static final int TYPE_LOGIN = 0;
	private static final int TYPE_REGIST = 1;
	
	/**
	 * 首页View,左侧view,右侧view,遮罩view
	 * */
	private View homeView,leftView,rightView,hideView;
	public LinearLayout mainView,certTypeParent;
	public static BCMHomeView bcmHomeView = null;
	/**
	 * SlideMenu菜单
	 * */
	public SlidingMenu mSlideMenu;
	
	/**
	 * 证件号码,手机号码,登录密码
	 * */
	private EditText certNumber,mobileNumber,secretNumber;
	/**
	 * 登录和注册按钮
	 * */
	private Button loginButton,registerButton;
	/**
	 * 判读是否是首次登陆
	 * */
	private boolean isFirstLogin = true;
	
	/**
	 * 登陆相关
	 * */
	private TextView certType,preMessage;
	private Dialog mCertDialog = null;
	private WheelView mCertWheel = null;
	private int index = 0;
	private int lastIndex = 0;
	
	/**
	 * 首页数据类
	 */
	public DataStorage mData = null;
	/**
	 * 判断当前哪个Menu打开，0-左侧，1-右侧
	 * */
	private int whichOpen = -1;
	
	
	public Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch(msg.what){
			case 0://关闭View
				hideView.setVisibility(View.GONE);
				leftView.setVisibility(View.GONE);
				rightView.setVisibility(View.GONE);
				hideView.setClickable(false);
				break;
			case 1://关闭View
				closeAllMenu();
				break;
			case 1000://建立加密通道离线资源更新成功
				//刷新视图
				LPMid.getInstance().um_.data.initOfflineJsonTwo();
				BCMHomeView.mSelectCity.resetGroups();
				BCMHomeView.mSelectCity.resetView();
				((DropGridAdapter)bcmHomeView.mDropGrids[0].getAdapter()).notifyDataSetChanged();
				bcmHomeView.mDropGrids[1].setAdapter(new DropGridAdapter(bv,false, LPMid.getInstance().um_.data.array2));
				bcmHomeView.mDropGrids[2].setAdapter(new DropGridAdapter(bv,false, LPMid.getInstance().um_.data.array3));
				bcmHomeView.mDropGrids[3].setAdapter(new DropGridAdapter(bv,false, LPMid.getInstance().um_.data.array4));

				bcmHomeView.mTopView.refresh();
				bcmHomeView.mTopView.updateWeather();
				if(null != progressDialog){
					progressDialog.cancel();					
				}
				this.sendEmptyMessageDelayed(1003, 1000);
				
				LPToastUtil.isBuildChannel = true;
				break;
			case 1001://建立加密通道离线资源更新失败
				if(null != progressDialog){
					progressDialog.cancel();					
				}
				String errorMsg = String.valueOf(msg.obj);
				showAlertDialog(false,errorMsg);
				break;
			case 1002://等待加密通道建立完成
				showAlertDialog(true,null);
				break;
			case 1003:
				if(LPMid.getInstance().um_.data.stepIntoPushMessage()){
					Intent intent = new Intent(BOCMainHome.this,BOCPushMessage.class);
					startActivity(intent);
				}
				break;
			}
			super.handleMessage(msg);
		}
	};
	
	private void handleJarEvent() {
		EventHandler uhandler = new EventHandler();
		uhandler.eventHandler = new Handler() {
			public void handleMessage(Message msg) {
				if (null != msg && msg.what == EventHandler.OFFLINE_FLASH) {
					Message msgg = new Message();
					msgg.what = 1000 ;
					handler.sendMessage(msgg);
					Utils.printOutToConsole("msg-------------------------1000");
				}
			}
		};
	}

	
	private BaseView bv = BOCMainHome.this;
	private ProgressDialog progressDialog = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_home);
		init();
		isFirstUse();
		isFirstLogin();
		setViewWithData();
		handleJarEvent();
	}
	
	/**
	 * 设置布局
	 */
	private void setViewWithData(){
		new Handler().post(new Runnable() {
			@Override
			public void run() {
				mSlideMenu.setMenuListener(new OnMenuListener() {
					@Override
					public void openMenu(boolean right) {
						// TODO Auto-generated method stub
						hideView.setVisibility(View.VISIBLE);
						hideView.setBackgroundColor(Color.TRANSPARENT);
						hideView.setClickable(true);
						whichOpen = right ? 1:0;
					}
					
					@Override
					public void closeMenu(boolean right) {
						// TODO Auto-generated method stub
						whichOpen = -1;
						handler.sendEmptyMessageDelayed(0, 600);
					}
				});			

				/**
				 * 超时后，进入首页打开侧边栏
				 */
				if(LPMid.getInstance().isTimeOut){
					new Handler().postDelayed(new Runnable() {
						
						@Override
						public void run() {
							mSlideMenu.showRightView();
						}
					}, 500);
					LPMid.getInstance().isTimeOut = false;
				}
			}
		});
		System.gc();
		System.gc();
		//建立加密通道
		connectHandShake();
	}
	/**
	 * 建立加密信道
	 */
	private void connectHandShake() {
		// TODO Auto-generated method stub
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				LPMid.getInstance().um_.handleFirstRequest(BOCMainHome.this,handler);						
			}
		}).start();
	}

	private void init() {
		mData = LPMid.getInstance().um_.data;
		mSlideMenu = (SlidingMenu)findViewById(R.id.slidingmenu);
		leftView = LayoutInflater.from(this).inflate(R.layout.layout_left_view, null);
		rightView = LayoutInflater.from(this).inflate(R.layout.layout_right_view, null);
		
		homeView = LayoutInflater.from(this).inflate(R.layout.layout_home_main, null);
		hideView = (View) homeView.findViewById(R.id.hideView);
		
		//添加主view
		LinearLayout bcmhome  = (LinearLayout)homeView.findViewById(R.id.homeview);
		bcmHomeView = new BCMHomeView(this);
		bcmhome.addView(bcmHomeView);
		
		leftView.setVisibility(View.INVISIBLE);
		rightView.setVisibility(View.INVISIBLE);
		
		//添加到SlidingMenu中
		mSlideMenu.addViews(leftView, homeView, rightView);
		
		LPUtils.scaledTHeight((LinearLayout)findViewById(R.id.left_top));
		TextView tv = (TextView)findViewById(R.id.right_top);
		tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, Utils.getScaledValueY(LPUtils.TITLE_HEIGHT)));
		
		createRightLoginView();
		createLeftSettingView();
		
		// 注册click事件
		certTypeParent.setOnClickListener(this);
		hideView.setOnClickListener(this);
		loginButton.setOnClickListener(this);
		registerButton.setOnClickListener(this);
		((ImageView)findViewById(R.id.setback)).setOnClickListener(this);
		((ImageView)findViewById(R.id.add_contact)).setOnClickListener(this);
	}

	/**
	 * 判断是否是非首次登陆
	 */
	private void isFirstLogin(){
		//BankUserCode不存在首次登陆
		if(TextUtils.isEmpty(LPMid.getInstance().um_.readBankUserCode())){
			isFirstLogin = true;
		}else{
			isFirstLogin = false;
		}
		
		//如果是非首次登陆,只显示登陆按钮和密码框
		if(!isFirstLogin){
			registerButton.setVisibility(View.GONE);
			((View)preMessage.getParent()).setVisibility(View.GONE);
			((View)certType.getParent()).setVisibility(View.GONE);
			((View)certNumber.getParent()).setVisibility(View.GONE);
			((View)mobileNumber.getParent()).setVisibility(View.GONE);
		}
	}

	/**
	 * 创建右侧登陆view
	 */
	private void createRightLoginView() {
		loginButton = (Button)findViewById(R.id.buttonLogin);
		registerButton = (Button)findViewById(R.id.buttonRegister);

		certTypeParent = (LinearLayout)findViewById(R.id.certificateParent);
		certType = (TextView)findViewById(R.id.certificateType);
		preMessage = (TextView)findViewById(R.id.preMessage);
		
		//默认选择第一个证件类型
		if(null != mData.certNames && mData.certNames.length > 0){
			certType.setText(mData.certNames[index]);			
		}
		certNumber = (EditText)findViewById(R.id.certificateNumber);
		mobileNumber = (EditText)findViewById(R.id.mobileNumber);
		secretNumber = (EditText)findViewById(R.id.secretNumber);
		//手机银行登陆框的父控件
		mainView = (LinearLayout)findViewById(R.id.main_view);
	}
	
	/**
	 * 创建左侧设置view
	 */
	private void createLeftSettingView() {
		new Handler().post(new Runnable() {
			@Override
			public void run() {
				LinearLayout settingMain = (LinearLayout)findViewById(R.id.setting_main);
				if(null != mData.settings && mData.settings.size() > 0){
					int size = mData.settings.size();
					for(int i = 0;i < size;i++){
						Map<String, Object> map = mData.settings.get(i);
						final String id = (String) map.get("id");
						String name = (String) map.get("name");
						final String url = (String) map.get("url");
						
						View view = (View) LayoutInflater.from(bv).inflate(R.layout.layout_left_view_setitem, null);
						ImageView icon = (ImageView)view.findViewById(R.id.icon_setting);
						TextView content = (TextView)view.findViewById(R.id.content_setting);
						Bitmap bitmap = Utils.getBitmapFromResources(bv, id);
						if(null != bitmap){
							icon.setImageBitmap(bitmap);
						}
						if(null != name){
							content.setText(getString(R.string.space)+name);
						}
						view.setOnClickListener(new OnClickListener() {
							
							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								doSettings(id,url);
							}
						});
						
						LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
						lp.topMargin = 5;
						lp.bottomMargin = 5;
						settingMain.addView(view,lp);
					}
				}
			}
		});
	}
	
	/**
	 * 创建证件类型的滚轮view
	 * */
	private void createCertTypWheelView() {
		View view = LayoutInflater.from(this).inflate(R.layout.layout_select_popup, null);
		
		if(null == mCertWheel || null == mCertDialog){
			LinearLayout layout = (LinearLayout)view.findViewById(R.id.wheelParent);
			
			mCertWheel = new WheelView(this);
			mCertWheel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
			mCertWheel.setAdapter(new ArrayWheelAdapter<String>(mData.certNames));
			layout.addView(mCertWheel);
			
			mCertDialog = new Dialog(this, R.style.popupAnimation);
			mCertDialog.setContentView(view);
			mCertDialog.setCancelable(false);
			//mCertDialog.setCanceledOnTouchOutside(true);
			
			Window mWindow = mCertDialog.getWindow();
			mWindow.setWindowAnimations(R.style.popupAnimation);
			mWindow.setGravity(Gravity.BOTTOM | Gravity.FILL_HORIZONTAL);
			mWindow.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
		}

		mCertWheel.addScrollingListener(new OnWheelScrollListener(){
			@Override
			public void onScrollingStarted(WheelView wheel) {
				// TODO Auto-generated method stub
			}
			@Override
			public void onScrollingFinished(WheelView wheel) {
				// TODO Auto-generated method stub
				index = wheel.getCurrentItem();
			}
		});
		
		((Button)view.findViewById(R.id.confirm)).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				certType.setText(mData.certNames[index]);
				mCertDialog.dismiss();
			}
		});
		
		((Button)view.findViewById(R.id.cancel)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				index = lastIndex;
				mCertWheel.setCurrentItem(index);
				mCertDialog.dismiss();
			}
		});
	}

	/**
	 * 关闭当前打开的Menu
	 * **/
	public void closeAllMenu(){
		if(whichOpen == 0){
			mSlideMenu.showLeftView();
		}else if(whichOpen == 1){
			mSlideMenu.showRightView();
		}
		
		//登陆成功之后隐藏view
		if(mData.isLogin){
			loginButton.setVisibility(View.GONE);
			registerButton.setVisibility(View.GONE);
			((View)secretNumber.getParent()).setVisibility(View.GONE);
			((View)certType.getParent()).setVisibility(View.GONE);
			((View)certNumber.getParent()).setVisibility(View.GONE);
			((View)mobileNumber.getParent()).setVisibility(View.GONE);
		}
		hideView.setVisibility(View.GONE);
		secretNumber.setText("");
	}

	/**
	 * 关闭弹出的软键盘
	 * */
	public void closeKeyBoard(){
		new Thread(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
				super.run();
				View view = getCurrentFocus();
				if(null != view){
					((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
					.hideSoftInputFromWindow(view.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);					
				}
				handler.sendEmptyMessage(1);
			}
		}.start();
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.hideView://遮罩view
			closeKeyBoard();
			break;
		case R.id.buttonLogin://登陆按钮
			if(!LPToastUtil.isBuildChannel){
				handler.sendEmptyMessage(1002);
				return;
			}
			checkForm(TYPE_LOGIN,mData.loginURL);
			break;
		case R.id.buttonRegister://注册按钮
			if(!LPToastUtil.isBuildChannel){
				handler.sendEmptyMessage(1002);
				return;
			}
			sendFormWithLogin(TYPE_REGIST, mData.registURL);
			break;
		case R.id.certificateParent://证件
			Utils.immUtils.setOnCloseImmListener(this,new onCloseImmListener() {
				@Override
				public void closeSuccess() {
					if(null == mCertWheel){
						createCertTypWheelView();							
					}
					if(null != mCertDialog && !mCertDialog.isShowing()){
						lastIndex = mCertWheel.getCurrentItem();
						mCertDialog.show();
					}
				}
			});
			break;
		case R.id.add_contact://选择联系人
			setPhoneLPTextField(mobileNumber);
			getPeopleConnects(this);
			break;
		case R.id.setback://左侧返回箭头
			mSlideMenu.showLeftView();
			break;
		}
	}
	
	/**
	 * 小i语音
	 */
	public void JumpBankcommVoice(){
		startActivity(new Intent(BOCMainHome.this,com.bankcomm.voice.BankCommVoiceActivity.class));
	};

	/**
	 * 登陆检查
	 * */
	private void checkForm(int type,String url){
		if(type == TYPE_LOGIN){
			String error = null;
			String cert  = certNumber.getText().toString();
			String mNumber = mobileNumber.getText().toString();
			String secret = secretNumber.getText().toString();
			
			if(isFirstLogin){
				if(null == cert || "".equalsIgnoreCase(cert)){
					error = certNumber.getHint().toString()+this.getString(R.string.not_null);
				}
				
				if(null == error){
					if(null != mNumber && !"".equalsIgnoreCase(mNumber)){
						if(mNumber.length() != 11){
							error = this.getString(R.string.hint4_error);
						}
					}else{
						error = mobileNumber.getHint().toString()+this.getString(R.string.not_null);						
					}
				}
			}

			if(error == null){
				if(null != secret && !"".equalsIgnoreCase(secret)){
					if(secret.length() < 6){
						error = this.getString(R.string.hint5_error);
					}
				}else{
					error = secretNumber.getHint().toString() + this.getString(R.string.not_null);;
				}
			}
			
			if(null == error && null != url){
				sendFormWithLogin(type,url);
			}else{
				LPMid.getInstance().alert(this, error, false);
			}
		}
	}
	
	/**
	 * 点击设置频道后的处理
	 * @param id
	 * @param url
	 */
	public void doSettings(String id,String url){
		if(!LPToastUtil.isBuildChannel){
			handler.sendEmptyMessage(1002);
			return;
		}
		if(null != url){
			Utils.printOutToConsole(id+url);
			if(null != id && id.equalsIgnoreCase("change_edtion")){
				Intent intent = new Intent(this,BCMEditionChange.class);
				startActivity(intent);
				handler.sendEmptyMessageDelayed(1,500);
			}else{
				LPMid.getInstance().um_.gotoEmpViewFromHome(this,url,"");				
			}
		}
	}
	
	/**
	 * 登陆提交form表单
	 * */
	private void sendFormWithLogin(final int type,final String url){
		LPMid.getInstance().waitDialog_.addFgTask(this, null, 0, 0, 0, new WaitDialog.Task(0) {

			String result = null;
			@Override
			public void run(WaitDialog dlg) throws Exception {
				HttpManager hm = LPMid.getInstance().hm_;
				String body = null;
				//登陆
				if (type == TYPE_LOGIN) {
					body = getFormBody(this);
					if(null != body){
						Utils.printOutToConsole("body --->  " + body);
						body = AESCipher.encrypt(body, AESCipher.clientKey_, AESCipher.clientIv_);						
					}
				}
				String uri = url;
				if(null != uri && uri.startsWith("/")){
					uri = ConfigManager.SERVER_URI + uri;
				}
				
				result = (String) hm.sendPostRequest(uri, body, this, null, null);
				if(null != result){
					result = AESCipher.decrypt(result, AESCipher.serverKey_, AESCipher.serverIv_);
					Utils.printOutToConsole("result --->  " + result);					
				}
			}

			@Override
			public void onSuccess(WaitDialog dlg) {
				if(null != result && !"".equalsIgnoreCase(result)){
					//登陆
					if(type == TYPE_LOGIN){
						try {
							mData.parseLoginMessage(result);
							if(mData.isLogin){
								handler.sendEmptyMessageDelayed(1,500);
							}
						} catch (Exception e) {
							handleException("登陆异常，请稍后再试！");
						}
					}else{
						Intent intent = new Intent(BOCMainHome.this,EMPView.class);
						intent.putExtra("reply", String.valueOf(result));
						BOCMainHome.this.startActivity(intent);
					}
				}
				super.onSuccess(dlg);
			}

			@Override
			public void onFailure(WaitDialog dlg) {
				// TODO Auto-generated method stub
				super.onFailure(dlg);
				handleException(getErrMsg());							
			}
		});
	}
	
	/**
	 * 弹出提示未登录信息，然后点却确定打开登陆侧边栏
	 * @param msg
	 */
	public void showToast(String msg){
		new AlertDialog.Builder(this).setTitle("温馨提示")
			.setMessage(msg)
			.setPositiveButton("确定", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					mSlideMenu.showRightView();
				}
			}).show();
	}
	
	/**
	 * 登陆成功后弹出登陆信息
	 */
	public void showLoginMessage(){
		//LPMid.getInstance().alert(this, mData.loginMessage, false);
		String showMessage ;
		if(mData.loginMessage == null||mData.loginMessage.equals("")){ //1月投产修改退出时提示信息
			showMessage ="您确定要退出交通银行客户端吗?";
		}else{
			showMessage = mData.loginMessage+"\n\n您确定要退出吗？" ;
		}
		new AlertDialog.Builder(this).setTitle("提示").setMessage(showMessage)
			.setPositiveButton("取消", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					
				}
			}).setNegativeButton("确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					exitSystem();
				}
			}).show();
	}
	
	/**
	 * 得到登录的body体，区分开首次登陆状态和非首次登录
	 * */
	public String getFormBody(Task task) throws Exception{
		String body = null;
		if(isFirstLogin){
			body = "certType=" + mData.certTypes[index]
					+ "&certNo=" + certNumber.getText().toString()
					+ "&usrMobileNo=" + mobileNumber.getText().toString()
					+ "&pin=" + Utils.escapeURIComponent(SecretUtil.encryptValue(secretNumber.getText().toString(), task));
		}else{
			body = "pin=" + Utils.escapeURIComponent(SecretUtil.encryptValue(secretNumber.getText().toString(), task));
		}
		return body;
	}
	
	@Override
	protected void onPause() {
		isExit = false;
		isHasTask = false;
		
		if(DropGridAdapter.isInEdit){
			bcmHomeView.restoreMyFavorite();
			BCMHomeView.changeTitleView(false);
		}
		super.onPause();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK){
			if(whichOpen >= 0){
				closeAllMenu();
			}else{
//				exitWithConfirm(); 
//				showLoginMessage();//1月投产
				
				backSystem();
			}
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
	
    /**
	 * 双击返回键退出应用程序
	 */
	private boolean isExit = false;
	private boolean isHasTask = false;
	
	private long exitTime = 0;
	
	Timer tExit = new Timer();

	/**
	 * 判断点击两次返回键退出系统
	 */
	private void backSystem(){
		TimerTask task = new TimerTask() {
			
			@Override
			public void run() {
				isExit = false;
				isHasTask = true;
			}
		};
		
		if (!isExit) {
			isExit = true;
			Toast.makeText(this, "再按一次退出交通银行手机银行", Toast.LENGTH_SHORT).show();
			exitTime = System.currentTimeMillis();
			if (!isHasTask) {
				tExit.schedule(task, 2000);
			}
		} else {
			long temp = System.currentTimeMillis() - exitTime;
			if (temp < 2000) {
				finish();
				System.exit(0);
			} else {
				Toast.makeText(this, "再按一次退出交通银行手机银行", Toast.LENGTH_SHORT).show();
				isExit = false;
				isHasTask = false;
			}
		}
	}
	
	/**
	 * 判断是否是首次使用
	 */
	private void isFirstUse(){
		String type = PackageManagerAndroid.ANDROIDDB.get("type");
		if (type.equals("") || type == null) {
			PackageManagerAndroid.ANDROIDDB.save("type", "first");
			hideView.setBackgroundResource(R.drawable.background1);
			hideView.setVisibility(View.VISIBLE);
		} else if (type != null && !type.equals("") && type.equals("first")) {
			hideView.setVisibility(View.GONE);
		}
	}
	
	/**
	 * 提示警告框
	 */
	public void showAlertDialog(boolean isSuccess,String errorMsg){
		if(isSuccess){
			progressDialog = new ProgressDialog(this);
			progressDialog.setTitle("温馨提示");
			progressDialog.setMessage("您的网络不给力，我们正在努力加载中");
			progressDialog.setProgress(R.drawable.wait_progress_loading);
			progressDialog.setButton("确定", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					progressDialog.dismiss();
				}
			});
			progressDialog.show();
		}else{
			new AlertDialog.Builder(bv).setTitle("温馨提示")
				.setMessage(errorMsg)
				.setCancelable(false)
				.setPositiveButton("重试", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					LPMid.getInstance().um_.handleFirstRequest(bv, handler);
				}
			}).setNegativeButton("退出", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					exitSystem();
				}
			}).show();
		}
	}
}